<?php
namespace Shop\Model;
use Think\Model;

class ShopQuickOrderModel extends Model{
	protected $tableName='shop_quick_order';
	protected $_validate = array(
		array('images','0,256','图片信息有误',Model::MUST_VALIDATE,'length'),
		array('typeid','/[1-9]\d*/','类型id参数错误',Model::EXISTS_VALIDATE),
		array('quantity','0,256','采购数量参数错误',Model::MUST_VALIDATE,'length'),
		array('connection','/[0-9]\d*/','联系方式参数错误',Model::MUST_VALIDATE),
		array('user_id','/[0-9]\d*/','user_id参数错误',Model::MUST_VALIDATE),
	);
	protected $_auto = array(
		array('create_time', NOW_TIME, Model::MODEL_INSERT),
		array('status', 1, Model::MODEL_INSERT),
	);

	public function edit_status_quick_order($ids,$status)
	{
		is_array($ids) ||
		$ids = explode(',',$ids);
		if($status == 2){
			$data['check_time'] = NOW_TIME;
		}
		$data['status'] = $status;
		return $this->where('id in ('.implode(',',$ids).')')->save($data);

	}

	public function add_or_edit_quick_order($quickorder)
	{
		if(!empty($quickorder['id'])){
			return	$this->save($quickorder);
		}else{
			return	$this->add($quickorder);
		}
	}

	public function get_quick_order_by_id($id){
		$ret =$this->where('id =' . $id)->find();
		if($ret['images']) $ret['images'] = explode(';', $ret['images']);
		return $ret;
	}

	public function get_quick_order_list($option){
		$where_str = '';
		empty($option['user_id']) || $where_arr[] = 'user_id = ' . $option['user_id'];
		empty($option['typeid']) || $where_arr[] = 'typeid = ' . $option['typeid'];

		if($option['status']) $where_arr[] = "status = " . $option['status'];
		else $where_arr[] = "status > 0 ";

		empty($where_arr) || $where_str .= implode(' and ', $where_arr);

		$order_str = '';
		$order_str = (empty($option['orderby']) ? 'create_time desc' : $option['orderby'].' desc');

		$option['page'] = (empty($option['page']) ? 1 : $option['page']);
		$option['r']    = (empty($option['r']) ? 10 : $option['r']);

		$ret['list']  = $this->where($where_str)->order($order_str)->page($option['page'], $option['r'])->select();
		foreach($ret['list'] as &$row){
			$row['images'] = explode(';', $row['images']);
			$row['main_image'] = $row['images'][0];
		}
		$ret['count'] = $this->where($where_str)->count();

		return $ret;
	}

	protected function _after_select(&$ret,$option)
	{

	}

	protected function _after_find(&$ret,$option)
	{

	}

}